package org.vergil.core.thread.productionmessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by vergil on 17-3-18.
 */
@Component
public class Consumer implements Runnable{

    @Autowired
    private Business business;

//    public Consumer(Business business){
//        this.business = business;
//    }

    @Override
    public void run() {

        while (true) {
            if (!business.getQueue().isEmpty()){
                try {
                    Map<String, Object> map =  new HashMap<>();
                    synchronized (this) {
                         map = business.getProduct();
                    }
                    System.out.println(Thread.currentThread().getName() + " get " + map.get("id") + " Product");
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
